#coding:utf-8
import sys
import os
import json
sys.path.append("matchutils")
from utils.utils import *
from algo.parse_name import *
from utils.wordtool import *

not_names= {}
delname_words="""中国|香港|台湾|澳门|内地|中国内地|大陆|其它|其他|未知|暂无|无|缺|暂缺|北京|不详|天津|上海|浙江|其他栏目|其它栏目|其它频道|editors|editor|日本|美国|印度|韩国|日本|印尼|马来西亚|新加坡|泰国|淘气|馒头|花卷|华语|华语群星|群星|新年|少年|无名|日本群星|无不知|无不能|无不想|无不不会|名著|主演|导演|欧美|欧洲|加拿大|男演员|女演员|好莱坞|好莱坞群星|宝莱坞|江苏籍影视演员|包子|男|女|内地男演员|法国|德国|英国|意大利|俄罗斯
"""
delname_words = delname_words.split('|')
for word in delname_words:
    word = word.strip()
    if not word: continue
    not_names[word] = 1

alias_names = {}
f = open(dict_dir + '/ent_map.txt', 'rb')
for line in f:
    line = line.strip()
    if line == '': continue
    line = line.lower()
    fields = line.split('\t')
    if len(fields) != 2: continue
    w1, w2 = fields
    w1 = gpa._convdigit( w1 )
    w2 = gpa._convdigit( w2 )

    if w1 not in alias_names:
        alias_names[w1] = []
    alias_names[w1].append(w2)

    if w2 not in alias_names:
        alias_names[w2] = []
    alias_names[w2].append(w1)
f.close()

def is_english_title( title ):
    return is_english_line( title )

def get_pname_keywords( allname ):
    if not allname:
        return []
    if isinstance(allname, unicode): 
        allname = allname.encode('utf-8', 'ignore')

    allname = allname.replace('演员','').replace('(女)', '')
    allname = allname.replace('/', ';').replace('、', ';')
    allname = allname.replace(',', ';').replace('，',';')
    allname = allname.replace('（歌手）', '').replace('|', ';')
    allname = allname.replace('（', ';').replace('）',';')
    allname = allname.replace('(', ';').replace(')',';')
    allname = allname.replace(',',';')
    allname = allname.split(';')
    names = [ name.strip().lower() for name in allname if name.strip() != ""] 
    names = list(set(names))
    result = []
    for name in names:
        if isinstance(name,unicode): 
            name = name.encode('utf-8', 'ignore')
        name = name.replace('-', '').replace('·','').replace('•','')
        name = name.replace('－', '').replace(' ','').replace('—','')
        name = name.replace("'", '')
        name = name.replace("・", '')
        if len(name) <= 5: continue
        if name in not_names : continue
        if name.isdigit(): continue
        result.append( name )
    return result


def split_othernames( name_ot ):
    if not name_ot:
        return []

    if isinstance(name_ot, unicode):
        name_ot = name_ot.encode('utf-8', 'ignore')
    
    name_ot = name_ot.replace('爱拍电影', '')
    name_ot = name_ot.replace('爱拍电影短片', '')
    name_ot = name_ot.strip()
    if not name_ot: return []
    othernames = name_ot.replace(',',';').replace('|',';')
    othernames = othernames.replace('/',';').replace('、',';')
    othernames = othernames.split(';')
    result = []
    for name in othernames:
        name = name.strip()
        if name == "": continue
        result.append( name )
    return result
    

def get_name_keywords( namelist ):
    stops = {'第':1, '部':1, '季':1, '的':1, '集':1, '之':1, '篇':1, 'of':1}
    if not namelist:
        return []
    keywords = []

    extwords = wordTool.extract_keywords( namelist[0] )

    tmp = []
    tmp.extend( namelist )
    for name in namelist:
        if name in alias_names:
            tmp.extend( alias_names[name] )
        #切词
        #####################################
        #####################################
    tmp.extend( extwords ) 

    newnamelist = list(set(tmp))
    for name in newnamelist:
        if isinstance(name, unicode):
            name = name.encode('utf-8', 'ignore')
        if not name: continue
        words, infos = gpa.parse_words( name )
        newwords = []
        if len(words) == 1:
            newwords.append( words[0]['word'] )
        elif len(words) > 0:
            for w in words:
                if w['flag'] == 'w': continue
                w = w['word'].strip()
                if w == '': continue
                if w == '的': continue
                if w == 'a': continue
                newwords.append( w )

        name = '' .join( newwords )
        t = re.sub('[0-9a-z]+$', '', name )
        if name == '我叫mt':
            pass
        elif len(t) >= 6 and t != '广场舞': 
            name = t
        if name in stops: continue
        if len( name ) <= 2: continue
        #if len(name.decode('utf-8', 'ignore')) == 1: continue
        keywords.append( name )

    keywords.extend( extwords )
    keywords = list(set( keywords ))
    if len(keywords) == 0 and len(namelist) > 0:
        name = namelist[0].lower()
        if isinstance(name, unicode):
            name = name.encode('utf-8', 'ignore')
        keywords.append( name )
    return keywords


if __name__ == '__main__':
    #print '\t'.join(get_name_keywords(['上帝保佑美国 God Bless America']))
    xname = '秋香 未删减版'
    xname = '天龙八部[未删减版]'
    xname = '蜡笔小新-3分钟百变大进击'
    xname = '鞭炮i'
    print '@@@'.join( get_name_keywords( [ "家" ] ) ), '======='
    #sys.exit()
    f = open('/letv/zoujiaqi/AlbumMatch/scores.txt')
    for line in f:
        line = line.strip()
        if not line: continue
        fields = line.split('\t')
        if len(fields) < 4: continue
        xname = fields[1]
        print '@@@'.join( get_name_keywords( [ xname ] ) )
    f.close()
